﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System.Globalization;
using FlexiCommerce.Configuration;

namespace FlexiCommerce.Web
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                System.Collections.Generic.IList<MetaTable> visibleTables = Global.DefaultModel.VisibleTables;
                EngineSection engine = FlexiCommerce.Context.Current.Resolve<EngineSection>();
                if (!engine.EnableGlobalization || !FlexiCommerce.Context.LocalizationManager.UseSystemLanguagePersistence)
                {
                    if(FlexiCommerce.Context.LocalizationManager.UseSystemLanguagePersistence == false)
                        visibleTables.Remove(MetaModel.Default.GetTable(typeof(Components.Language)));
                }
                if (visibleTables.Count == 0)
                {
                    throw new InvalidOperationException("There are no accessible tables. Make sure that at least one data model is registered in Global.asax and scaffolding is enabled or implement custom pages.");
                }
                Menu1.DataSource = visibleTables;
                Menu1.DataBind();
            }
        }

    }
}
